//
//  BaseXmlModel.m
//  ymall
//
//  Created by yuike6 yuike6 on 12-2-6.
//  Copyright (c) 2012年 yuike. All rights reserved.
//

#import "BaseXmlModel.h"

@interface BaseXmlModel() {
@private
}

-(BOOL) localFileChecke: (NSURL *) xmlFile;
@end




@implementation BaseXmlModel
@synthesize version = version_;
@synthesize date = date_;

@synthesize name = name_;
@synthesize description = description_;

@synthesize filePath = filePath_;


@synthesize xmlTree = xmlTree_;
@synthesize nodeID=nodeID_;


-(void) dealloc
{
    YRelease(version_);
    YRelease(date_);
    
    YRelease(name_);
    YRelease(description_);
    YRelease(nodeID_)
    
    YRelease(xmlTree_);
    YRelease(filePath_)
    
    [super dealloc];    
}


-(NSString*) description {
    if(!description_) {
        return description_;
    }
    NSString* temp = description_;
    temp = [temp replace:@"\r" with:@""];
    while ([temp rangeOfString:@"\n\n"].length  > 0) {
        temp = [temp replace:@"\n\n" with:@"\n"];
    }
    return temp.strip;
}


-(BOOL) localFileChecke: (NSURL *) xmlFile
{
    if(xmlFile == nil)
    {
        return FALSE;
    }
    
    if(![xmlFile isFileURL])
    {
        return FALSE;
    }
    return TRUE;
}

-(BOOL) isParsedOk
{
    return self.xmlTree != nil;
}


-(id) initWithXmlLocalPath:(NSURL *) xmlLocalPath
{
    self = [self init];
    if(self)
    {
        self.filePath = [xmlLocalPath absoluteString];
        if(![self localFileChecke: xmlLocalPath])
        {
            // 不能网络访问
            return nil;
        }
        
        self.xmlTree = [XMLParser XMLParsedTreeFromURL:xmlLocalPath];
        
        if(!self.xmlTree)
        {
            return nil;
        }       
                
    }    
    return self;
}

-(id) initWithXmlTree:(XMLParsedTree *) tree
{    
    if(!tree)
    {
        return nil;
    }
    
    self = [self init];
    if(self)
    {
        self.xmlTree = tree;
    }
    
    return self;
}

- (id) initWithData:(NSData *) data
{
    if (!data) {
        return nil;
    }
    
    self = [self init];
    if(self)
    {
        self.xmlTree = [XMLParser XMLParsedTreeFromData:data];
    }
    
    return self;
    
}
@end
